#pragma once

#include <iostream>
#include <string>
#include <functional>
#include "InetAddr.hpp"
#include <memory>

class EpollServer;


// 对文件描述符进行封装
// 普通的 fd Listensockfd
// 对 fd 的处理方式采用同一种方式
class Connection
{
public:
    Connection(): _sockfd(-1), _events(0)
    {}

    void SetPeerInfo(const InetAddr &peer_addr)
    {
        _peer_addr = peer_addr;
    } 

    void SetSockfd(int sockfd)
    {
        _sockfd = sockfd;
    }

    int Sockfd(){return _sockfd;}
    void SetEvents(uint32_t events){_events = events;}
    uint32_t GetEvents(){return _events;}

    void SetOwner(EpollServer *owner)
    {
        _owner = owner;
    }

    EpollServer* GetOwner()
    {
        return _owner;
    }

    // 回调方法
    virtual void Sender() = 0;
    virtual void Recver() = 0;
    virtual void Excepter() = 0;

    ~Connection()
    {}

protected:
    int _sockfd;
    // 输入输出缓冲区
    std::string _inbuffer;
    std::string _outbuffer;

    InetAddr _peer_addr; // 对应哪个客户端

    // 添加一个指针
    EpollServer* _owner;
    // 当前关心的事件
    uint32_t _events; 
};


// class Factory
// {
// public:
//     static std::shared_ptr<Connection> BuildConnection(int fd, uint32_t events, func_t r, func_t s, func_t e)
//     {
//         auto conn = std::make_shared<Connection>(fd);
//         conn->SetEvents(events);
//         conn->InitCB(r, s, e);
//         return conn;
//     }
// };